package com.zjitc.controller;


import com.zjitc.entity.Product;
import com.zjitc.service.ProductService;
import com.zjitc.utils.ResponseResult;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:徐莹
 * @Date:2025/04/14
 * 商品控制器
 */
@RestController
@RequestMapping("/product")
@PreAuthorize("hasAuthority('product:admin')")
public class ProductController {

    @Autowired
    private ProductService productService;
    
    @Autowired
    private CacheManager cacheManager;

    /**
     * 查询所有商品 - 使用/All路径
     * 使用缓存提高性能
     */
    @GetMapping("/All")
    public ResponseResult<List<Product>> getAllProducts() {
        List<Product> productList = productService.findAllProducts();
        if (productList != null) {
            return ResponseResult.success(productList);
        }
        return ResponseResult.error("没有商品");
    }
    
    /**
     * 根据ID查询商品
     * 使用缓存提高性能
     */
    @GetMapping("/{id}")
    public ResponseResult<Product> getProductById(@PathVariable int id) {
        Product product = productService.findProductById(id);
        if (product != null) {
            return ResponseResult.success(product);
        } else {
            return ResponseResult.error("商品不存在");
        }
    }

    /**
     * 添加商品
     * 自动更新相关缓存
     */
    @PostMapping("/add")
    public ResponseResult<Product> addProduct(@RequestBody Product product) {
        try {
            Product addedProduct = productService.addProduct(product);
            return ResponseResult.success("添加商品成功", addedProduct);
        } catch (Exception e) {
            return ResponseResult.error("添加商品失败：" + e.getMessage());
        }
    }

    /**
     * 更新商品
     * 自动更新相关缓存
     */
    @PutMapping("/update")
    public ResponseResult<Product> updateProduct(@RequestBody Product product) {
        try {
            Product updatedProduct = productService.updateProduct(product);
            return ResponseResult.success("更新商品成功", updatedProduct);
        } catch (Exception e) {
            return ResponseResult.error("更新商品失败：" + e.getMessage());
        }
    }

    /**
     * 删除商品
     * 自动更新相关缓存
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Void> deleteProduct(@PathVariable int id) {
        try {
            productService.deleteProduct(id);
            return ResponseResult.success("删除商品成功", null);
        } catch (Exception e) {
            return ResponseResult.error("删除商品失败：" + e.getMessage());
        }
    }

    /**
     * 强制刷新所有商品缓存
     * 用于手动刷新缓存
     */
    @PostMapping("/refreshCache")
    public ResponseResult<Void> refreshProductCache() {
        try {
            // 清除后会自动重新加载
            cacheManager.getCache("product").clear();
            // 预热列表缓存
            productService.findAllProducts();
            return ResponseResult.success("商品缓存已刷新", null);
        } catch (Exception e) {
            return ResponseResult.error("刷新缓存失败：" + e.getMessage());
        }
    }
} 